﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Semantics.Base;
namespace Semantics.Parse
{
    public class LangNodeTraverser : IEnumerator<LangNode>
    {
        LangNode RootNode;
        public List<LangNode> LangNodesSequence = new List<LangNode>();
        int currentindex = -1;
        public LangNodeTraverser(LangNode rootnode)
        {
            this.RootNode = rootnode;
            Enroll(rootnode);
        }

        void Enroll(LangNode langnode)
        {
            for (int i = 0; i < langnode.Children.Count; i++)
                Enroll(langnode.Children[i]);
            LangNodesSequence.Add(langnode);
        }
        LangNode currentlangnode;
        public LangNode Current
        {
            get
            {
                if (this.currentindex == -1)
                    throw new Exception("Please call MoveNext befere get Current!");
                return this.currentlangnode;
            }
        }

        object IEnumerator.Current { get { return this.currentlangnode; } }
        public void Reset()
        {
            currentlangnode = null;
            this.currentindex = -1;
        }
        public bool MoveNext()
        {
            if (currentindex == this.LangNodesSequence.Count - 1)
                return false;
            currentindex++;
            this.currentlangnode = this.LangNodesSequence[this.currentindex];
            return true;
        }

        void IDisposable.Dispose()
        {
        }
    }
}
